package commom;
 
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.*;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.edge.EdgeOptions;
 
 
import java.awt.*;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
 
/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:龙宝
 * Date:2023-04-15
 * Time:13:02
 */
public class AutoTestUtils {

    public static EdgeDriver driver;

    //创建驱动对象
    public static EdgeDriver createDriver() {
        if(driver == null) {
            EdgeOptions options = new EdgeOptions();
            options.addArguments("--remote-allow-origins=*");
            driver = new EdgeDriver(options);

            //创建隐式等待
            driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(3));
        }
        return driver;
    }


    //保存屏幕截图
    public static void getScreen(String str) throws IOException {
        List<String> list = getTime();

        //dir + filename
        //./指的是当前的项目路径下
        String filename = "./src/test/java/screen/" + list.get(0) + "/" + str + "_" + list.get(1) + ".png";
        File srcFile = driver.getScreenshotAs(OutputType.FILE);
        //放到指定位置
        FileUtils.copyFile(srcFile,new File(filename));
    }

    private static List<String> getTime() {
        //文件按照天的维度按文件进行保存
        //文件格式 20231108-12345毫秒
        SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyyMMdd-HHmmssSSS");
        SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyyMMdd");

        String filename = simpleDateFormat1.format(System.currentTimeMillis());
        String dirname = simpleDateFormat2.format(System.currentTimeMillis());

        List<String> list = new ArrayList<>();
        list.add(dirname);
        list.add(filename);
        return list;
    }


}